﻿#include "relatedlg.h"
#include <QtGui/QComboBox>
#include <QString>
#include <QListWidgetItem>
#include <QVariant>
#include <QMessageBox>


RelateDlg::RelateDlg(cColumn_Info *pColumnInfo, unsigned int OldRelateId, unsigned int CurRecordId, QWidget *parent) :QDialog(parent)
{
	m_pTimer = NULL;
	memset((unsigned int*)&m_ColumnInfo, 0, sizeof(cColumn_Info));
	m_AppType = 0;
	m_CurTableId = 0;
	m_CurColumnId = 0;
	m_RelateTableId = 0;
	m_bSymmetry = false;
	m_CurRecordId = 0;
	m_OldRelateId = 0;
	m_NewRelateId = 0;

	m_RltId1=0;
	m_RltId2=0;
	m_RltId3=0;
	if (pColumnInfo)
	{
		memcpy((unsigned int*)&m_ColumnInfo, (unsigned int*)pColumnInfo, sizeof(cColumn_Info));
		m_AppType = m_ColumnInfo.AppType;
		m_CurTableId = m_ColumnInfo.TableId;
		m_CurColumnId = m_ColumnInfo.ColumnId;
		m_RelateTableId = m_ColumnInfo.clmRltTableId;
		m_bSymmetry = m_ColumnInfo.clmIsbeSingleRelate;	//是否对称

	}
	m_OldRelateId = OldRelateId;
	m_NewRelateId = OldRelateId;
	m_CurRecordId = CurRecordId;

	setupUi(this);
	setFixedSize(643, 354);
	setModal(true);
	pshBtn_Clear->setText(QString::fromLocal8Bit("清 除"));
    pshBtn_Affirm->setText(QString::fromLocal8Bit("确 定"));
    pshBtn_Cancel->setText(QString::fromLocal8Bit("取 消"));
	label_CurSlt->setText(QString::fromLocal8Bit("当前选择"));
	lineEdit_CurSlt->setReadOnly(true);
	RelateTreeWdgt->setSortingEnabled(false);
	RelateTreeWdgt->setDragEnabled(false);
	RelateTreeWdgt->setAutoScroll(true);
	RelateTreeWdgt->setColumnCount(1);
	RelateTreeWdgt->setHeaderHidden(true);
	RelateTreeWdgt->setRootIsDecorated(true);
	RelateTreeWdgt->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	RelateTreeWdgt->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	RelateListWdgt->setAlternatingRowColors(true);
	pStateLabel->setText("");
	
	connect(pshBtn_Clear, SIGNAL(clicked()), this, SLOT(ClearRelate()));
	connect(pshBtn_Affirm, SIGNAL(clicked()), this, SLOT(AffirmRelate()));
	connect(pshBtn_Cancel, SIGNAL(clicked()), this, SLOT(CancelRelate()));
	connect(RelateTreeWdgt, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(SelectTreeShow(QTreeWidgetItem*, int)));	
	connect(RelateListWdgt, SIGNAL(itemClicked(QListWidgetItem* )), this, SLOT(SelectListShow(QListWidgetItem*)));

	m_pTimer = new QTimer(this);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(OnTimerProc()));
	m_pTimer->start(50);

	//InitDlgShow();
	pStateLabel->setText(QString::fromLocal8Bit("正在初始化..."));
}

RelateDlg::~RelateDlg()
{

}


void RelateDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) 
	{
    case QEvent::LanguageChange:
        retranslateUi(this);
        break;
    default:
        break;
    }
}

void RelateDlg::OnTimerProc()
{
	if (m_pTimer)
	{
		m_pTimer->stop();
		InitDlgShow();
		pStateLabel->setText(QString::fromLocal8Bit("初始化完成"));
	}
}
//设置对话框标题
void RelateDlg::SetDlgTitl()
{ 
	QString		szTitle;
	QString		CurTableName;
	QString		CurRecordName;
	QString		CurColName;
	QString		CurSltCont;
	int	Ret = 0;
	Ret = g_DbCfgFunObj.GetTableChnNamebyAppType_TableId(m_AppType, m_CurTableId,CurTableName);
	Ret = g_DbCfgFunObj.GetRecordNamebyAppType_TableId_RecordId(m_AppType, m_CurTableId, m_CurRecordId, CurRecordName);
	Ret = g_DbCfgFunObj.GetColChnNamebyAppType_TableId_ColId(m_AppType, m_CurTableId, m_CurColumnId, CurColName);

	szTitle = QString::fromLocal8Bit("表:[%1],记录[%2],字段[%3]的关联设置").arg(CurTableName).arg(CurRecordName).arg(CurColName);
	setWindowTitle(szTitle);
	CurSltCont = QString::fromLocal8Bit("当前“%1”的选择：").arg(CurColName);
}

void RelateDlg::InitDlgShow()
{
	if (m_RelateTableId <=0 || m_CurTableId <=0 || m_CurRecordId<=0 || m_CurColumnId<=0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("关联错误信息"), QString::fromLocal8Bit("域名表中关联信息设置有误！"));
		return;
	}
	
	SetDlgTitl();
	SetCurSltContShow(m_AppType, m_RelateTableId, m_OldRelateId);

	if (TABLE_RTASK_INFO == m_RelateTableId ||
		TABLE_RTRANSMITCONFIG_INFO == m_RelateTableId ||
// 		TABLE_RPARAMS_YX == m_RelateTableId ||
// 		TABLE_RPARAMS_YK == m_RelateTableId ||
		TABLE_RPARAMS_NET == m_RelateTableId )
	{
		RelateTreeWdgt->setEnabled(false);
		InitListShow(0);
	}
	else
	{
		InitTreeShow(SYSTEM_TYPE_NSA_YD,TABLE_RTASK_INFO);
	}
}

void RelateDlg::SelectTreeShow(QTreeWidgetItem *pItem, int ColumnNo)
{
	if (pItem)
	{
		RelateListWdgt->clear();
		unsigned int NewData = pItem->data(0, Qt::UserRole).toUInt();

		if (NewData>0)
		{
			int SltTableId = NewData/PARENT_OFFSET;
			if (TABLE_RTASK_INFO == SltTableId && 
				TABLE_RDEVS_INFO == m_RelateTableId)
			{
				InitListShow(NewData);
			}
			if (TABLE_RDEVS_INFO == SltTableId)
			{

				if (TABLE_RDEVS_YX == m_RelateTableId || TABLE_RDEVS_YC == m_RelateTableId || 
					TABLE_RDEVS_YK == m_RelateTableId )
				{
					InitListShow(NewData);
				}
			}
		}
	}
}

void RelateDlg::InitTreeShow(int AppType, int TableId)
{
	RelateTreeWdgt->clear();

	QTreeWidgetItem *pTaskItem = NULL;

	char			*buff = NULL;
	int			buff_len = 0;
	int			RetNum = 0;
	int			Fs_Id = 0;
	int			i = 0;
	QString			ItemText = 0;
	int			ItemData = 0;

	pTaskItem = new QTreeWidgetItem(RelateTreeWdgt);

	ItemText = QString::fromLocal8Bit("终端");
	ItemData = PARENT_OFFSET*TABLE_RPARAMS_TERMINAL;
	pTaskItem->setText(0, ItemText);
	pTaskItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	pTaskItem->setData(0, Qt::UserRole, ItemData);
	pTaskItem->setIcon(0, QIcon(":/images/open.png"));
	if (TABLE_RDEVS_YX == m_RelateTableId || TABLE_RDEVS_RYB == m_RelateTableId ||
		TABLE_RDEVS_YC == m_RelateTableId || TABLE_RDEVS_BHYC == m_RelateTableId ||
		TABLE_RDEVS_YK == m_RelateTableId ||
		TABLE_RDEVS_YM == m_RelateTableId)
	{
		InitTreeDeviceItem(ItemData, pTaskItem);
	}

	RetNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, 
																   TABLE_RTASK_INFO, 
																   buff,
																   &buff_len, true);

	if (RetNum>0 && buff && buff_len>0)
	{
		for (i=0; i<RetNum; i++)
		{
			pTaskItem = new QTreeWidgetItem(RelateTreeWdgt);

			ItemText = QString::fromLocal8Bit(((rTask_Info*)buff)[i].Name);
			ItemData = ((rTask_Info*)buff)[i].ID+PARENT_OFFSET*TABLE_RTASK_INFO;
			pTaskItem->setText(0, ItemText);
			pTaskItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
			pTaskItem->setData(0, Qt::UserRole, ItemData);
			pTaskItem->setIcon(0, QIcon(":/images/open.png"));
			if (TABLE_RDEVS_YX == m_RelateTableId || TABLE_RDEVS_RYB == m_RelateTableId ||
				TABLE_RDEVS_YC == m_RelateTableId || TABLE_RDEVS_BHYC == m_RelateTableId ||
				TABLE_RDEVS_YK == m_RelateTableId ||
				TABLE_RDEVS_YM == m_RelateTableId)
			{
				InitTreeDeviceItem(ItemData, pTaskItem);
			}
		}
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
}

void RelateDlg::InitTreeDeviceItem(int ParentId, QTreeWidgetItem *pParentItem)
{
	char	*buff = NULL;
	int	buff_len = 0;
	int	Result = 0;
	int	i = 0;
	QTreeWidgetItem *pItem = NULL;
	QString	ItemText;
	int	ItemData = 0;

	Result = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, 
																	TABLE_RDEVS_INFO, 
																	ParentId,
																	buff,
																	&buff_len, true);
	if (Result>0 && buff && buff_len>0)
	{
		for (i=0; i<Result; i++)
		{
			if (TABLE_RDEVS_INFO == m_CurTableId && 
				TABLE_RDEVS_YX == m_RelateTableId)
			{
				/*if (m_CurRecordId != ((rDevS_Info*)buff)[i].ID)
				{
					continue;
				}*/
			}

			pItem = new QTreeWidgetItem(pParentItem);
			ItemText = QString::fromLocal8Bit(((rDevS_Info*)buff)[i].Name);
			ItemData = ((rDevS_Info*)buff)[i].ID+PARENT_OFFSET*TABLE_RDEVS_INFO;
			pItem->setText(0, ItemText);
			pItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
			pItem->setData(0, Qt::UserRole, ItemData);
			pItem->setIcon(0, QIcon(":/images/open.png"));
			/*if (TABLE_RDEVS_YX == m_RelateTableId ||
				TABLE_RDEVS_YC == m_RelateTableId ||
				TABLE_RDEVS_YK == m_RelateTableId ||
				TABLE_RDEVS_YM == m_RelateTableId)
			{
				InitTreeDeviceItem(ItemData, pItem);
			}*/
		}
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
}

void RelateDlg::InitListShow(int ParentId)
{
	if (SYSTEM_TYPE_NSA_YD != m_AppType )
	{
		return;
	}

	char			*buff = NULL;
	int			buff_len = 0;
	int			Num = 0;
	QString			ItemText = 0;
	RelateListWdgt->clear();

	if (ParentId>0)
	{
		Num = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_AppType, m_RelateTableId, ParentId, buff, &buff_len, true);
	}else
	{
		Num = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(m_AppType, m_RelateTableId, buff, &buff_len, true);
	}
	
	if (Num>0 && buff && buff_len>0)
	{
		if (TABLE_RDEVS_YX == m_RelateTableId)
		{
			SetYxList(Num, buff);
		}else if (TABLE_RDEVS_YK == m_RelateTableId)
		{
			SetYkList(Num,buff);
		}else if (TABLE_RTASK_INFO == m_RelateTableId && TABLE_RTASK_INFO == m_CurTableId)
		{
			SetTaskList(Num, buff);
		}else if (TABLE_RTRANSMITCONFIG_INFO == m_RelateTableId)
		{
			SetTransmitList(Num,buff);
		}
// 		else if (TABLE_RPARAMS_YX == m_RelateTableId)
// 		{
// 			SetParamsYxList(Num,buff);
// 		}else if (TABLE_RPARAMS_YK == m_RelateTableId)
// 		{
// 			SetParamsYkList(Num,buff);
// 		}
		else if (TABLE_RPARAMS_NET == m_RelateTableId)
		{
			SetParamsNetList(Num,buff);
		}
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
}

//显示当前选择任务的列表
void RelateDlg::SetTaskList(int RecordNum, char *buff)
{
	if (RecordNum<=0 || !buff)
	{
		return;
	}
	int i = 0; 

	rTask_Info CurTaskInfo;
	QListWidgetItem *pItem = NULL;
	QString			ItemText = 0;
	int			ItemData = 0;
	for (i=0; i<RecordNum; i++)
	{
		if (m_CurRecordId == ((rTask_Info*)buff)[i].ID)
		{
			memcpy((unsigned int *)&CurTaskInfo, (unsigned int *)(((rTask_Info*)buff)+i), sizeof(rTask_Info));
			break;
		}
	}

	for ( i=0; i<RecordNum; i++)
	{
		if ( ((rTask_Info*)buff)[i].ID != m_CurRecordId && 
			((rTask_Info*)buff)[i].m_ProtType == CurTaskInfo.m_ProtType)
		{
			if (g_DbCfgFunObj.CurTaskIsSub(((rTask_Info*)buff)[i].ID) &&
				!g_DbCfgFunObj.CurSubTaskIsRelated(((rTask_Info*)buff)[i].ID))
			{
				ItemText = QString::fromLocal8Bit(((rTask_Info*)buff)[i].Name);
				ItemData = ((rTask_Info*)buff)[i].ID + PARENT_OFFSET*TABLE_RTASK_INFO;
				pItem = new QListWidgetItem(RelateListWdgt);
				pItem->setText(ItemText);
				pItem->setData(Qt::UserRole, QVariant(ItemData));
				RelateListWdgt->addItem(pItem);
			}
		}
	}
}

//显示当前转发表的列表
void RelateDlg::SetTransmitList(int RecordNum, char *buff)
{
	if (RecordNum<=0 || !buff)
	{
		return;
	}
	int i = 0; 

	rTransmitConfig_Info CurTransmitConfigInfo;
	QListWidgetItem *pItem = NULL;
	QString			ItemText = 0;
	int			ItemData = 0;

	for ( i=0; i<RecordNum; i++)
	{
		ItemText = QString::fromLocal8Bit(((rTransmitConfig_Info*)buff)[i].Name);
		ItemData = ((rTransmitConfig_Info*)buff)[i].ID + PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO;
		pItem = new QListWidgetItem(RelateListWdgt);
		pItem->setText(ItemText);
		pItem->setData(Qt::UserRole, QVariant(ItemData));
		RelateListWdgt->addItem(pItem);
	}
}

//显示当前选择遥信的列表
void RelateDlg::SetYxList(int RecordNum, char *buff)
{
	if (RecordNum<=0 || !buff)
	{
		return;
	}

	int i = 0; 
	QListWidgetItem *pItem = NULL;
	QString			ItemText = 0;
	int			ItemData = 0;
	char			TableName[TABLE_ENG_NAME_LEN];
	bool			bCheck = false;
	
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
		TABLE_RDEVS_YX == m_RelateTableId)
	{
		if (TABLE_RTASK_INFO == m_CurTableId)
		{
			g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(m_AppType, m_CurTableId, TableName);
			for (i=0; i<RecordNum; i++)
			{
				if (!g_DbCfgFunObj.CurRecordIsUsedInColRelate(((rDevS_Yx*)buff)[i].ID, m_RelateTableId, TableName, m_ColumnInfo.ColumnEngName))
				{
					ItemText = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
					ItemData = ((rDevS_Yx*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
					pItem = new QListWidgetItem(RelateListWdgt);
					pItem->setText(ItemText);
					pItem->setData(Qt::UserRole, QVariant(ItemData));
					RelateListWdgt->addItem(pItem);
				}
			}

		}else if (TABLE_RDEVS_YX == m_CurTableId)
		{
			sprintf(TableName, "rDevS_Yx");
			if (0 == QString(QString::fromLocal8Bit(m_ColumnInfo.ColumnEngName)).compare("YxDoubleId", Qt::CaseInsensitive) )
			{
				bCheck = true;
			}
			for (i=0; i<RecordNum; i++)
			{
				if (m_CurRecordId == ((rDevS_Yx*)buff)[i].ID)
				{
					continue;
				}
				if (bCheck && g_DbCfgFunObj.CurRecordIsUsedInColRelate(((rDevS_Yx*)buff)[i].ID, m_RelateTableId, TableName, m_ColumnInfo.ColumnEngName))
				{
					continue;
				}
				ItemText = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
				ItemData = ((rDevS_Yx*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
				pItem = new QListWidgetItem(RelateListWdgt);
				pItem->setText(ItemText);
				pItem->setData(Qt::UserRole, QVariant(ItemData));
				RelateListWdgt->addItem(pItem);
			}	
		}else if (TABLE_RDEVS_YK == m_CurTableId)
		{
			g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(m_AppType, m_CurTableId, TableName);
			for (i=0; i<RecordNum; i++)
			{
				if (!g_DbCfgFunObj.CurRecordIsUsedInColRelate(((rDevS_Yx*)buff)[i].ID, m_RelateTableId, TableName, m_ColumnInfo.ColumnEngName))
				{
					ItemText = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
					ItemData = ((rDevS_Yx*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
					pItem = new QListWidgetItem(RelateListWdgt);
					pItem->setText(ItemText);
					pItem->setData(Qt::UserRole, QVariant(ItemData));
					RelateListWdgt->addItem(pItem);
				}
			}
		}
		else if (TABLE_RTRANSMIT_YK == m_CurTableId)
		{
			g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(m_AppType, m_CurTableId, TableName);
			for (i=0; i<RecordNum; i++)
			{ 
				if (!g_DbCfgFunObj.CurRecordIsUsedInColRelate(((rDevS_Yx*)buff)[i].ID, m_RelateTableId, TableName, m_ColumnInfo.ColumnEngName))
				{
					ItemText = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
					ItemData = ((rDevS_Yx*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
					pItem = new QListWidgetItem(RelateListWdgt);
					pItem->setText(ItemText);
					pItem->setData(Qt::UserRole, QVariant(ItemData));
					RelateListWdgt->addItem(pItem);
				}
			}
		}
		//ccg17.5.25 61850模型配置dataobject
		else if (TABLE_RDATA_OBJECT == m_CurTableId)
		{
			g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(m_AppType, m_CurTableId, TableName);
			for (i=0; i<RecordNum; i++)
			{ 
				if (!g_DbCfgFunObj.CurRecordIsUsedInColRelate(((rDevS_Yx*)buff)[i].ID, m_RelateTableId, TableName, m_ColumnInfo.ColumnEngName))
				{
					ItemText = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
					ItemData = ((rDevS_Yx*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
					pItem = new QListWidgetItem(RelateListWdgt);
					pItem->setText(ItemText);
					pItem->setData(Qt::UserRole, QVariant(ItemData));
					RelateListWdgt->addItem(pItem);
				}
			}
		}
	}
}

//	显示当前选择遥控的列表
void RelateDlg::SetYkList(int RecordNum, char *buff)
{
	if (RecordNum<=0 || !buff)
	{
		return;
	}

	int i = 0; 
	QListWidgetItem *pItem = NULL;
	QString			ItemText = 0;
	int			ItemData = 0;

	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
		TABLE_RDEVS_YK == m_RelateTableId)
	{
		if (TABLE_RDEVS_YX == m_CurTableId )
		{
			for (i=0; i<RecordNum; i++)
			{
				if ((0 == QString(QString::fromLocal8Bit(m_ColumnInfo.ColumnEngName)).compare("RelYkID", Qt::CaseInsensitive) &&
					!g_DbCfgFunObj.CurYKIsUsedInYx(((rDevS_Yk*)buff)[i].ID) ))
				{
					ItemText = QString::fromLocal8Bit(((rDevS_Yk*)buff)[i].Name);
					ItemData = ((rDevS_Yk*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
					pItem = new QListWidgetItem(RelateListWdgt);
					pItem->setText(ItemText);
					pItem->setData(Qt::UserRole, QVariant(ItemData));
					RelateListWdgt->addItem(pItem);
				}
			}
		}
	}
}

//显示当前选择遥信的列表
// void RelateDlg::SetParamsYxList(int RecordNum, char *buff)
// {
// 	if (RecordNum<=0 || !buff)
// 	{
// 		return;
// 	}
// 
// 	int i = 0; 
// 	QListWidgetItem *pItem = NULL;
// 	QString			ItemText = 0;
// 	int				ItemData = 0;
// 	char			TableName[TABLE_ENG_NAME_LEN];
// 	bool			bCheck = false;
// 
// 	memset(TableName, 0, TABLE_ENG_NAME_LEN);
// 	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
// 		TABLE_RPARAMS_YX == m_RelateTableId)
// 	{
// 		if (TABLE_RPARAMS_YX == m_CurTableId)
// 		{
// 			sprintf(TableName, "rParamS_Yx");
// 			if (0 == QString(QString::fromLocal8Bit(m_ColumnInfo.ColumnEngName)).compare("MapYxInf", Qt::CaseInsensitive) )
// 			{
// 				bCheck = true;
// 			}
// 			for (i=0; i<RecordNum; i++)
// 			{
// 				if (m_CurRecordId == ((rParamS_Yx*)buff)[i].ID)
// 				{
// 					continue;
// 				}
// 				if (bCheck && g_DbCfgFunObj.CurRecordIsUsedInColRelate(((rParamS_Yx*)buff)[i].ID, m_RelateTableId, TableName, m_ColumnInfo.ColumnEngName))
// 				{
// 					continue;
// 				}
// 				ItemText = QString::fromLocal8Bit(((rParamS_Yx*)buff)[i].Name);
// 				ItemData = ((rParamS_Yx*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
// 				pItem = new QListWidgetItem(RelateListWdgt);
// 				pItem->setText(ItemText);
// 				pItem->setData(Qt::UserRole, QVariant(ItemData));
// 				RelateListWdgt->addItem(pItem);
// 			}	
// 		}
// 	}
// }


//	显示当前选择遥控的列表
// void RelateDlg::SetParamsYkList(int RecordNum, char *buff)
// {
// 	if (RecordNum<=0 || !buff)
// 	{
// 		return;
// 	}
// 
// 	int i = 0; 
// 	QListWidgetItem *pItem = NULL;
// 	QString			ItemText = 0;
// 	int			ItemData = 0;
// 
// 	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
// 		TABLE_RPARAMS_YK == m_RelateTableId)
// 	{
// 		if (TABLE_RPARAMS_YX == m_CurTableId )
// 		{
// 			for (i=0; i<RecordNum; i++)
// 			{
// 				if ((0 == QString(QString::fromLocal8Bit(m_ColumnInfo.ColumnEngName)).compare("RelYkId", Qt::CaseInsensitive) &&
// 					!g_DbCfgFunObj.CurYKIsUsedInYx(((rParamS_Yk*)buff)[i].ID) ))
// 				{
// 					ItemText = QString::fromLocal8Bit(((rParamS_Yk*)buff)[i].Name);
// 					ItemData = ((rParamS_Yk*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
// 					pItem = new QListWidgetItem(RelateListWdgt);
// 					pItem->setText(ItemText);
// 					pItem->setData(Qt::UserRole, QVariant(ItemData));
// 					RelateListWdgt->addItem(pItem);
// 				}
// 			}
// 		}
// 	}
// }


//	显示当前选择遥控的列表
void RelateDlg::SetParamsNetList(int RecordNum, char *buff)
{
	if (RecordNum<=0 || !buff)
	{
		return;
	}

	int i = 0; 
	QListWidgetItem *pItem = NULL;
	QString			ItemText = 0;
	int			ItemData = 0;

	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
		TABLE_RPARAMS_NET == m_RelateTableId)
	{
		if (TABLE_RTASK_INFO == m_CurTableId )
		{
			for (i=0; i<RecordNum; i++)
			{
				ItemText = QString::fromLocal8Bit(((rParamS_Net*)buff)[i].Name);
				ItemData = ((rParamS_Net*)buff)[i].ID + PARENT_OFFSET*m_RelateTableId;
				pItem = new QListWidgetItem(RelateListWdgt);
				pItem->setText(ItemText);
				pItem->setData(Qt::UserRole, QVariant(ItemData));
				RelateListWdgt->addItem(pItem);

			}
		}
	}
}


void RelateDlg::SetCurSltContShow(int AppType, int TableId, int CurRelateId)
{
	QString CurSltName;
	CurSltName = QString::fromLocal8Bit("无");
	
	lineEdit_CurSlt->setText(CurSltName);
	if (AppType<=0 || TableId<=0 || CurRelateId<=0)
	{
		return ;
	}
	int	Result = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	int	RecordId = CurRelateId%PARENT_OFFSET;
	Result = g_DbCfgFunObj.GetRecordNamebyAppType_TableId_RecordId(AppType, TableId, RecordId, buff, &buff_len);
	if (Result>0 && buff)
	{
		CurSltName = QString::fromLocal8Bit(buff);
		lineEdit_CurSlt->setText(CurSltName);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
	return;
}


void RelateDlg::SelectListShow(QListWidgetItem *pItem)
{
	if (pItem)
	{
		int NewData = pItem->data(Qt::UserRole).toInt();
		if (NewData > 0)
		{
			m_NewRelateId = NewData;
			SetCurSltContShow(m_AppType, m_RelateTableId, m_NewRelateId);
		}else
		{
			m_NewRelateId = m_OldRelateId;
		}
	}
}

void RelateDlg::ClearRelate()
{
	m_NewRelateId = 0;
	SetCurSltContShow(m_AppType, m_RelateTableId, m_NewRelateId);
	SaveRelateData();
	close();
}

void RelateDlg::AffirmRelate()
{
	SetCurSltContShow(m_AppType, m_RelateTableId, m_NewRelateId);
	SaveRelateData();
	close();
}

void RelateDlg::CancelRelate()
{
	m_NewRelateId = m_OldRelateId;
	SetCurSltContShow(m_AppType, m_RelateTableId, m_NewRelateId);
	close();
}

int RelateDlg::GetRelateData(unsigned int &RelateId, QString &ItemText)
{
	RelateId = 0;
	RelateId = m_NewRelateId;
	ItemText = lineEdit_CurSlt->text();
	return 1;
}

int RelateDlg::GetRecordId(int &RecordId1, int &RecordId2,int &RecordId3)
{
	RecordId1 = 0;
	RecordId2 = 0;
	RecordId3 = 0;

	RecordId1 = m_RltId1;
	RecordId2 = m_RltId2;
	RecordId3 = m_RltId3;
	return 1;
}

//	保存关联数据
void RelateDlg::SaveRelateData()
{
	if (m_NewRelateId == m_OldRelateId)
	{
		return;
	}

	if (SYSTEM_TYPE_NSA_YD != m_AppType ||
		m_CurTableId <=0 || 
		m_RelateTableId <=0 ||
		m_CurRecordId <= 0)
	{
		return;
	}
	bool bSingleRlt = false;

	int UpdateRet = 0;
	switch (m_CurTableId)
	{
	case TABLE_RTASK_INFO:
		{
			if (m_RelateTableId==TABLE_RTASK_INFO)
			{
				UpdateRet = SaveSameTableRelateInfo();
			}
			else
			{
				UpdateRet = SaveSingleRelateInfo();
			}
		}
		break;
	case TABLE_RDEVS_YX:
		{
			if (TABLE_RDEVS_YX == m_RelateTableId)
			{
				if (0 == QString(m_ColumnInfo.ColumnEngName).compare("YxDoubleId", Qt::CaseInsensitive))
				{
					UpdateRet = SaveSameTableRelateInfo();
				}else /*if (0 == QString(m_ColumnInfo.ColumnEngName).compare("yxAccidentYxId", Qt::CaseInsensitive))*/
				{
					if (m_bSymmetry) //对称配置
					{
						UpdateRet = SaveSameTableRelateInfo();
					}else
					{
						UpdateRet = SaveSingleRelateInfo();
						bSingleRlt = true;
					}
				}
			}else if (TABLE_RDEVS_YK == m_RelateTableId)
			{
				UpdateRet = SaveSingleRelateInfo();
			}
		}
		break;
	case TABLE_RDEVS_YK:
		{
			if (TABLE_RDEVS_YX == m_RelateTableId)
			{
// 				if (0 == QString(m_ColumnInfo.ColumnEngName).compare("RelateYxId", Qt::CaseInsensitive))
// 				{
// 					UpdateRet = SaveDifferTableRelateInfo();
// 				}else 
				if (0 == QString(m_ColumnInfo.ColumnEngName).compare("StationEnaYxId", Qt::CaseInsensitive) ||
					0 == QString(m_ColumnInfo.ColumnEngName).compare("WfLockYxId", Qt::CaseInsensitive) )
				{
					UpdateRet = SaveSingleRelateInfo();
					bSingleRlt = true;
				}
			}
		}
		break;
	default:
		{
			if (m_bSymmetry)
			{
				if (m_CurTableId == m_RelateTableId)
				{
					UpdateRet = SaveSameTableRelateInfo();
				}
			}else
			{
				UpdateRet = SaveSingleRelateInfo();
				bSingleRlt = true;
			}
			
		}
		break;
	}

	if (UpdateRet>0)
	{
		m_RltId1 = m_CurRecordId;
		if (m_RelateTableId == m_CurTableId && !bSingleRlt)
		{
			m_RltId2 = m_OldRelateId%PARENT_OFFSET;
			m_RltId3 = m_NewRelateId%PARENT_OFFSET;
		}
	}
}

//	保存同一表的相互关联
int RelateDlg::SaveSameTableRelateInfo()
{
	if (SYSTEM_TYPE_NSA_YD != m_AppType ||
		m_CurTableId <=0 || 
		m_CurRecordId <= 0)
	{
		return -1;
	}
	if (m_RelateTableId != m_CurTableId)
	{
		return -1;
	}
	unsigned int NewRltData = 0;
	int  Result = 0;
	QString Str;
	bool	bPerOperate = false;
	switch (m_CurTableId)
	{
	case TABLE_RDEVS_YX:
		{
			Str = QString::fromLocal8Bit("遥信");
		}
		break;
	case TABLE_RTASK_INFO:
		{
			Str = QString::fromLocal8Bit("任务");
		}
		break;
	default:
		break;
	}
	if (m_OldRelateId>0)
	{
		unsigned int OldeRltRecordId = 0;
		OldeRltRecordId = m_OldRelateId%PARENT_OFFSET;
		if (OldeRltRecordId>0)
		{
			NewRltData = 0;
			if (TABLE_RTASK_INFO == m_CurTableId)
			{
				Result = g_DbCfgFunObj.ConfigConegerAndMasState(m_AppType, m_CurTableId, OldeRltRecordId, NewRltData, true);
			}else
			{
				Result = g_DbCfgFunObj.UpDateOneColumnData(&m_ColumnInfo, OldeRltRecordId, (char*)&NewRltData, sizeof(unsigned int));
			}
			
			if (Result<=0)
			{
				QMessageBox::StandardButton ret;
				QString Strtemp;
				
				Strtemp = QString::fromLocal8Bit("清空原备%1的[%2]失败！是否继续操作？").arg(Str).arg(QString(m_ColumnInfo.ColumnChnName));

				ret = QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), Strtemp, QMessageBox::Yes | QMessageBox::No);
				if (ret != QMessageBox::Yes)
				{
					return -2;
				}
			}
		}
	}

	if (m_NewRelateId > 0)
	{
		unsigned int NewRltRecordId = 0;
		unsigned int TempRltId = 0;
		NewRltRecordId = m_NewRelateId%PARENT_OFFSET;

		if (NewRltRecordId>0 )
		{
			TempRltId = m_CurRecordId+PARENT_OFFSET*m_CurTableId;
			if (TABLE_RTASK_INFO == m_CurTableId)
			{
				Result = g_DbCfgFunObj.ConfigConegerAndMasState(m_AppType, m_CurTableId, NewRltRecordId, TempRltId, false);
			}else
			{
				Result = g_DbCfgFunObj.UpDateOneColumnData(&m_ColumnInfo, NewRltRecordId, (char*)&TempRltId, sizeof(unsigned int));
			}
			if (Result<=0)
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("设置备%1的[%2]失败！").arg(Str).arg(QString::fromLocal8Bit(m_ColumnInfo.ColumnChnName))  );
				return -2;
			}
		}
	}

	Result = g_DbCfgFunObj.UpDateOneColumnData(&m_ColumnInfo, m_CurRecordId, (char*)&m_NewRelateId, sizeof(unsigned int));
	
	if (Result<=0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("设置本%1的[%2]失败！").arg(Str).arg(QString(m_ColumnInfo.ColumnChnName)));
		return -2;
	}

	return 1;
}

//	保存单一关联
int RelateDlg::SaveSingleRelateInfo()
{
	if (SYSTEM_TYPE_NSA_YD != m_AppType ||
		m_CurRecordId<=0 || 
		m_RelateTableId <=0 || 
		m_CurTableId <=0)
	{
		return -1;
	}
	int Result = 0;

	Result = g_DbCfgFunObj.UpDateOneColumnData(&m_ColumnInfo, m_CurRecordId, (char*)&m_NewRelateId, sizeof(unsigned int));

	if (Result<=0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("关联[%1]失败！").arg(QString(m_ColumnInfo.ColumnChnName)));
		return -2;
	}

	if (m_CurTableId==TABLE_RTRANSMIT_YK)
	{
		unsigned int	TaskIdYxLock=0;
		unsigned int	DevIdYxLock=0;
		unsigned int	IndexNoYxLock=0;
		
		Result = 0;
		Result = g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YX,m_NewRelateId,TaskIdYxLock,DevIdYxLock,IndexNoYxLock);

		if (Result>0)
		{
			g_DbCfgFunObj.ConfigYkTransmitAndYx(m_CurRecordId,TaskIdYxLock,DevIdYxLock,IndexNoYxLock);
		}
		else
		{
			return -2;
		}
	}

	return 1;
}

